 ;(function($) {
 
 var OnImage = {
 
	tools: {
		pan: function(handler, toolbar) {
			
			$viewport = handler.viewport();
			
			//$viewport.unbind('mousedown').mousedown(function(e0) {
			$viewport.unbind('mousedown').mousedown(function(e0) {

				// MDOMENECH TODO: find a better way to do this
				var $ot = $(e0.originalTarget);
				if ($ot.hasClass('ui-resizable-handle')) return;
				
				handler._start();
				
				if (e0.preventDefault) e0.preventDefault();
				else e0.returnValue = false;
				
				var t0 = handler.event(e0);				
				var ts = t0;
				var scale0 = handler.status().scale;
				
				var p = handler.canvas().position();
				
				handler.originvp(p.left, p.top, scale0);
				
				//var e0 = handler.event(e);
				
				$(document).mousemove(function(e1) {
					if (e1.preventDefault) e1.preventDefault();
					else e1.returnValue = false;
					
					var t1 = handler.event(e1);
					
					var dx = e1.pageX - e0.pageX;
					var dy = e1.pageY - e0.pageY;
					
					if (e1.shiftKey == true) {
						var scale = scale0 * (1 - dy / 100);
						handler.scaleTo(scale, t0.click.cx, t0.click.cy);
					}
					else {
						handler.translateTo(p.left + dx, p.top + dy);
					}
					
				}).mouseup(function() {
					handler.origin(false);					
					$(this).unbind("mousemove").unbind("mouseup").unbind("mouseout");
					handler._end();
				});
			}).css({cursor: 'move'});
		},
		
		zoom: function(handler, toolbar) {
			$viewport = handler.viewport();			
			if (!$viewport.mousewheel) return;
			
			$viewport.unbind('mousewheel').mousewheel(function(e, delta) {
				var t0 = handler.event(e);
				if (e.preventDefault) e.preventDefault();
				else e.returnValue = false;
				
				handler.scaleTovp(handler.status().scale * (1 + delta/10), t0.click.vpx, t0.click.vpy);
				//handler.scaleTovp(handler.status().scale+delta/10, t0.click.vpx, t0.click.vpy);
				//handler.scalevp(1+delta/10, t0.click.vpx, t0.click.vpy);
			});			
		}
	}
	
}

window.OnImage = OnImage;
	
})(jQuery);	